package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

public class Index extends Activity implements OnClickListener {

	private Authentication_DAO datasource;

	Button library, lms,pagol, timetable, results;
	TextView logout, lecturerIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_index);

		library = (Button) findViewById(R.id.button2);
		lms = (Button) findViewById(R.id.LMS);
		pagol = (Button) findViewById(R.id.Pagol);
		timetable = (Button) findViewById(R.id.timetable);
		results = (Button) findViewById(R.id.Results);
		logout = (TextView) findViewById(R.id.txtlogout);

		library.setOnClickListener(this);
		lms.setOnClickListener(this);
		pagol.setOnClickListener(this);
		timetable.setOnClickListener(this);
		results.setOnClickListener(this);
		logout.setOnClickListener(this);

		datasource = new Authentication_DAO(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.index, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == library) {
			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_lib_perUser); //GlobalData.autherntication_id_lib+ Integer.parseInt(GlobalData.username)
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, Login_Activity.class);
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/gims/library/Logon.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk/gims/library/Logon.asp&SID="
						+ respones.getResultsMap().get("SID") + "&APPID=";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("title", "//title---");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2
						.put("txtUserName", authentication.getuser_name());
				parametersMap2.put("txtPassword", authentication.getpassword());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk/gims/library/Logon.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response response2 = null;
				try {
					response2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					// Intent i = new Intent(this, Login_Activity.class);
					// startActivity(i);
					e.printStackTrace();
				}

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/gims/library/Links.asp");

				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3.put("title", "/html[1]/body[1]/p[4]/a[1]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				try {
					respones3 = bpelProcess3.doRequest(request3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (respones3.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones3.getResultsMap().get("title");

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, Library.class);
					i.putExtra("title", testResult);
					startActivity(i);
				} else {

					AlertDialog.Builder alt = new
							AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, Login_Activity.class);
					startActivity(i);
				}
			}
			datasource.close();
		}
		if (v == lms) {
			datasource.open();

			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_lmsnew_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, LMSLogin.class);
				startActivity(i);
			} else {

				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://lms.apiit.lk/login/index.php";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("logout",
						"/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[2]---");
				xpathMap2
						.put("link",
								"/html[1]/body[1]/div[2]/div[2]/div[1]/div[1]/a[1]---href");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();
				// username=cb004229&password=aaAA12!@
				parametersMap2.put("username", authentication.getauthentication_name());
				parametersMap2.put("password", authentication.getpassword());

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;
				try {
					respones2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (respones2.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones2.getResultsMap().get("test");
					String link = respones2.getResultsMap().get("link");

					Toast.makeText(getApplicationContext(), testResult + link,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, LMS.class);
					i.putExtra("logout", testResult);
					i.putExtra("link", link);
					startActivity(i);
				} else {
					AlertDialog.Builder alt = new
							AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, LMSLogin.class);
					startActivity(i);
				}
			}
			datasource.close();
		}
		if (v == results) {
			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_results_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, ResultsLogin.class);
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/gims/result/logon.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk/gims/result/logon.asp&SID="
						+ respones.getResultsMap().get("SID") + "&APPID=";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2
						.put("studNumber", "/html[1]/body[1]/input[2]---value");
				xpathMap2.put("SID", "/html[1]/body[1]/input[4]---value");
				request2.setXpathMap(xpathMap2);

				// txtUserName=cb004229&txtPassword=uparcdra&REDIRECTPAGE=http%3A%2F%2Fwebspace1.apiit.lk%2Fgims%2Fresult%2Flogon.asp&SID=758770417&APPID=&Submit=Login
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2.put("txtUserName", authentication.getuser_name());
				parametersMap2.put("txtPassword",authentication.getpassword());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk/gims/result/logon.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;
				try {
					respones2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/gims/result/mainresults.asp?STUDENT_NUMBER="
						+ respones2.getResultsMap().get("studNumber")
						+ "&SID="
						+ respones2.getResultsMap().get("SID"));
				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3.put("title", "/html[1]/body[1]/select[1]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				// parametersMap2.get("txtUsername");
				try {
					respones3 = bpelProcess3.doRequest(request3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Process bpelProcess4 = new BPEL_Process();
				BPEL_Request request4 = new BPEL_Request();
				request4.setUrl("http://webspace1.apiit.lk/gims/result/mainresults.asp?STUDENT_NUMBER="
						+ respones2.getResultsMap().get("studNumber")
						+ "&SID="
						+ respones2.getResultsMap().get("SID"));
				Map<String, String> xpathMap4 = new HashMap<String, String>(); // /html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[2]
				xpathMap4
						.put("batch1",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[1]---");
				xpathMap4
						.put("batch2",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[2]---");
				xpathMap4
						.put("batch3",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[3]---");
				xpathMap4
						.put("icpassport", "/html[1]/body[1]/input[9]---value"); // /html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[1]
				xpathMap4
						.put("scriptVal1",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[1]---href");
				xpathMap4
						.put("scriptVal2",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[2]---href");
				xpathMap4
						.put("scriptVal3",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[2]/a[3]---href");
				request4.setXpathMap(xpathMap4);

				// CAMPUS=CB&icpassport=&student_number=CB004229&appraisal=&SID=60ad25a0e8f444c6282e8d1512a8963b
				Map<String, String> parametersMap4 = new HashMap<String, String>();
				parametersMap4.put("CAMPUS", "CB");
				parametersMap4.put("student_number", respones2.getResultsMap()
						.get("studNumber"));
				parametersMap4.put("SID", respones2.getResultsMap().get("SID"));
				parametersMap4.put("icpassport", "");
				parametersMap4.put("appraisal", "");
				request4.setParametersMap(parametersMap4);

				request4.setType("POST");
				BPEL_Response respones4 = null;
				try {
					respones4 = bpelProcess4.doRequest(request4);

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (respones4.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String batch1 = respones4.getResultsMap().get("batch1");
					String batch2 = respones4.getResultsMap().get("batch2");
					String batch3 = respones4.getResultsMap().get("batch3");
					String icpassport = respones4.getResultsMap().get(
							"icpassport");

					String scriptVal1 = respones4.getResultsMap().get(
							"scriptVal1");
					String scriptVal2 = respones4.getResultsMap().get(
							"scriptVal2");
					String scriptVal3 = respones4.getResultsMap().get(
							"scriptVal3");

					String studNumber = respones2.getResultsMap().get(
							"studNumber");
					String SID = respones2.getResultsMap().get("SID");

					Toast.makeText(getApplicationContext(), icpassport,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, ResultsIndex.class);
					i.putExtra("batch1", batch1);
					i.putExtra("batch2", batch2);
					i.putExtra("batch3", batch3);
					i.putExtra("icpassport", icpassport);
					i.putExtra("scriptVal1", scriptVal1);
					i.putExtra("scriptVal2", scriptVal2);
					i.putExtra("scriptVal3", scriptVal3);
					i.putExtra("studNumber", studNumber);
					i.putExtra("SID", SID);
					startActivity(i);
				} else {
					AlertDialog.Builder alt = new
							AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, ResultsLogin.class);
					startActivity(i);
				}
			}
			datasource.close();
		}
		if (v == pagol) {
			// Link---- http://webspace1.apiit.lk/pagol15/www/
			datasource.open();
			Authentication authentication = datasource
					.getAction(GlobalData.authentication_id_pagol_perUser);
			boolean isRegistered = authentication != null;
			if (!isRegistered) {
				Intent i = new Intent(this, PagolLogin.class);
				// i.putExtra("", respones.getResultsMap().get("name"));
				startActivity(i);
			} else {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/pagol15/www/login.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;
				try {
					respones = bpelProcess.doRequest(request);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk:80/pagol15/www/login.asp&SID="
						+ respones.getResultsMap().get("SID");
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("title", "//title---");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2
						.put("txtUserName", authentication.getuser_name());
				parametersMap2.put("txtPassword", authentication.getpassword());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk:80/pagol15/www/login.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;
				try {
					respones2 = bpelProcess.doRequest(request2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/pagol15/www/student_top.asp");

				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3
						.put("title",
								"/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[2]/td[1]/table[1]/tr[1]/td[1]/font[1]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				try {
					respones3 = bpelProcess3.doRequest(request3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (respones3.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {
					String testResult = respones3.getResultsMap().get("title");

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();

					Intent i = new Intent(this, Pagol.class);
					i.putExtra("title", testResult);
					startActivity(i);
				} else {
					AlertDialog.Builder alt = new
							AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, PagolLogin.class);
					startActivity(i);
				}
			}
			datasource.close();
		}
		if (v == timetable) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://timetable.apiit.lk/TT-Version17.0/");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("text",
					"//input[@name='javax.faces.ViewState']---value");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (respones.getStatus().equalsIgnoreCase(
					GlobalData.message_success)) {
				String testResult = respones.getResultsMap().get("text");

				Toast.makeText(getApplicationContext(), testResult,
						Toast.LENGTH_LONG).show();
				Intent i = new Intent(this, TimeTable.class);
				i.putExtra("text", testResult);
				startActivity(i);

			} else {

				Intent i = new Intent(this, Index.class);
				startActivity(i);
			}

		}

		if (v == logout) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace.apiit.lk/myspace.jsp?Action=LogOut");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put(
					"text",
					"/html[1]/body[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/li[1]/li[3]/div[1]/ul[1]/li[1]/a[1]/span[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (respones.getStatus().equalsIgnoreCase(
					GlobalData.message_success)) {
				String testResult = respones.getResultsMap().get("text");
				GlobalData.cookies = "";
				Toast.makeText(getApplicationContext(), testResult,
						Toast.LENGTH_LONG).show();

				Intent i = new Intent(this, MainActivity.class);
				i.putExtra("text", testResult);
				startActivity(i);
			} else {

				Intent i = new Intent(this, Index.class);
				startActivity(i);
			}
		}

	}

}
